﻿using System.Windows;
using System.Windows.Controls;
using ImageAppDataModels;
using ImageAppViewModels;
using BrainTechLLC;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for SelectSortControl.xaml
    /// </summary>
    public partial class SelectSortControl : UserControl
    {
        public static readonly DependencyProperty SettingsViewModelProperty =
            DependencyProperty.Register("SettingsViewModel", typeof(SettingsViewModel), typeof(SelectSortControl), new PropertyMetadata(null, OnSettingsViewModelPropertyChanged));

        public SettingsViewModel SettingsViewModel
        {
            get { return (SettingsViewModel)GetValue(SettingsViewModelProperty); }
            set { SetValue(SettingsViewModelProperty, value); }
        }

        private static void OnSettingsViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SelectSortControl source = d as SelectSortControl;
            SettingsViewModel vNew = e.NewValue as SettingsViewModel;
            SettingsViewModel vOld = e.OldValue as SettingsViewModel;
            source.UpdateSettingsViewModel(vNew, vOld);
        }

        private void UpdateSettingsViewModel(SettingsViewModel viewModel, SettingsViewModel viewModelOld)
        {
            if (viewModel == viewModelOld)
                return;

            if (viewModelOld != null)
            {
                viewModelOld.PropertyChanged -= viewModel_PropertyChanged;
            }

            viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModel_PropertyChanged);

            SettingsViewModel = viewModel;

            // Let the UI bind to the view-model.
            this.DataContext = SettingsViewModel;
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {            
        }

        public SelectSortControl()
        {
            InitializeComponent();            
        }

        private void btnResortNow_Click(object sender, RoutedEventArgs e)
        {
            //SettingsViewModel.RaisePropertyChanged("SortBy");
            Settings.Values.RaisePropertyChanged("SortBy");
        }        
    }
}
